//
//  AppDelegate.swift
//  传智微博
//
//  Created by zuxi li on 2017/3/14.
//  Copyright © 2017年 lingtu. All rights reserved.
//


import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking



@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    
    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        // 应用程序额外信息
        setupAddition()
        
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = WBMainViewController()
        window?.makeKeyAndVisible()
        
        loadAppInfo()
        return true
    }

    
}


// MARK: - 设置应用程序额外信息
extension AppDelegate {
    fileprivate func setupAddition() {
        // 设置AVProgressHUD 最小解除时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        // 设置网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        // 获得用户授权，显示通知（上方的提示条/ 声音 / badgeNumber）
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge, .carPlay]) { (success, error) in
                print("授权 " + (success ? "成功" : "失败"))
            }
        } else {
            // Fallback on earlier versions
            let notifySettings = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notifySettings)
        }

    }
}

// MARK: - 从服务器加载应用程序信息
extension AppDelegate {
    fileprivate func loadAppInfo() {
        DispatchQueue.global().async {
            // 1. URL 假数据
            let url = Bundle.main.url(forResource: "main", withExtension: ".json")
            // 2.转换成二进制数据
            let data = NSData(contentsOf: url!)
            // 3.写入磁盘
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
            // 直接保存在沙盒等待下一次程序的使用
            data?.write(toFile: jsonPath, atomically: true)
            
            print("应用程序加载完毕 \(jsonPath)")
        }
    }
    
    
    
    
    
    
    
}
